package com.lgy.ms.service.config;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;

import javax.annotation.PostConstruct;
import java.io.IOException;
import java.lang.reflect.Field;
import java.util.Enumeration;
import java.util.Properties;

/**
 * 全局配置
 */
@Configuration
public class GlobalNames {

    private static Logger logger = LoggerFactory.getLogger(GlobalNames.class);
    private static GlobalNames instance = null;

    @Value("${global-names.path}")
    private String configPath;

    public static int login_errorChance = 5;
    public static int login_freezeMin = 30;

    public static int TIMEOUT_SYSUSER_SESSIONINFO = 60 * 60 * 48;//缓存两天时间
    public static int TIMEOUT_USER_SESSIONINFO = 60 * 60 * 48;//缓存两天时间
    public static int TIMEOUT_APPSESSIONID = 60 * 60 * 24;

    public static String download_zip_file_path = "D:/temp/";
    public static String download_zip_urlPrefix = "http://localhost:6666/ms/file/";
    public static String file_path_split = "file/";

    public static String upload_file_path = "D:/temp/";
    public static int upload_file_size = 10485760;
    public static String upload_format_limit = "gif,jpg,jpeg,png,bmp";
    public static boolean upload_check = false;
    public static String upload_urlPrefix = "http://localhost:6666/ms/file/";

    // 读取配置文件
    @SuppressWarnings("rawtypes")
    @PostConstruct
    public GlobalNames getInstance() {
        if (instance == null) {
            synchronized (GlobalNames.class) {
                if (instance == null) {
                    instance = new GlobalNames();
                    Resource resource = new ClassPathResource(configPath);
                    if (resource.exists()) {
                        Properties properties = new Properties();
                        try {
                            properties.load(resource.getInputStream());
                            @SuppressWarnings("unchecked")
                            Enumeration<String> proEnum = (Enumeration<String>) properties.propertyNames();
                            while (proEnum.hasMoreElements()) {
                                String key = proEnum.nextElement();
                                String value = properties.getProperty(key);
                                try {
                                    Field field = GlobalNames.class.getField(key);
                                    Class fieldType = field.getType();
                                    if (fieldType == int.class
                                            || fieldType == Integer.class) {
                                        field.set(null, Integer.parseInt(value));
                                    } else if (fieldType == float.class
                                            || fieldType == Float.class) {
                                        field.set(null, Float.parseFloat(value));
                                    } else if (fieldType == double.class
                                            || fieldType == Double.class) {
                                        field.set(null,Double.parseDouble(value));
                                    } else if (fieldType == long.class
                                            || fieldType == Long.class) {
                                        field.set(null, Long.parseLong(value));
                                    } else if (fieldType == boolean.class
                                            || fieldType == Boolean.class) {
                                        Boolean tmpValue = value.toUpperCase().equals("TRUE") ? Boolean.TRUE : Boolean.FALSE;
                                        field.set(null, tmpValue);
                                    } else {
                                        field.set(null, value);
                                    }
                                } catch (NoSuchFieldException e) {
                                    logger.warn("属性文件[{}]中属性为：{} 在GlobalNames中不存在！", configPath, key);
                                } catch (IllegalAccessException e) {
                                    logger.error(String.format("设置GlobalNames的属性%s时出错"), e);
                                }
                            }
                        } catch (IOException e) {
                            logger.error("加载配置属性文件[{}]失败，原因为{}", configPath, e.getMessage());
                        }
                    } else {
                        logger.warn("配置属性文件[{}]不存在！", configPath);
                    }
                }
            }
        }
        return instance;
    }


}
